﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;
using Skyworth.CRM.Service.Entity;

namespace Skyworth.CRM.Web.CustomerManagement
{
    public partial class EditSalesLeads : BasePage
    {
        private ICustBussinessoppBLL bussBLL = IocContainer.Instance.Resolve<ICustBussinessoppBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            SystemOverTime();
            if (!IsPostBack)
            {
                //hidMakerId.Value = base.UserID;
                //this.txtMaker.Text = base.UserName;
                this.txtDate.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
                if ("update" == Request.QueryString["Type"].ToString().ToLower())
                {
                    int leadId = 0;
                    int.TryParse(Request.QueryString["LeadId"], out leadId);
                    if (leadId > 0)
                    {
                        DataTable dt = bussBLL.Query(string.Format(" and FItemId={0}", leadId));
                        if (dt.Rows.Count > 0)
                        {
                            DataRow dr = dt.Rows[0];
                            this.txtCode.Text = dr["FNumber"].ToString();// model.FNumber;
                            this.txtName.Text = dr["FComName"].ToString();// model.FComName;
                            this.txtStrength.Text = dr["FStrength"].ToString();// model.FStrength;
                            this.txtAddress.Text = dr["FAddress"].ToString();// model.FAddress;
                            this.hidEmpId.Value = dr["FEmpId"].ToString();
                            this.txtEmployee.Text = dr["EmpName"].ToString();// model.FEmpId.ToString();
                            this.txtArea.Text = dr["FArea"].ToString();// model.FArea;
                            this.txtChannel.Text = dr["FChannel"].ToString();// model.FChannel;
                            this.txtProducts.Text = dr["FProducts"].ToString();// model.FProducts;
                            this.txtWebsite.Text = dr["FWebsite"].ToString();// model.FWebsite;
                            this.txtNote.Text = dr["FNote"].ToString();// model.FNote;
                            this.txtDate.Text = dr["FDate"].ToString() == "" ? "" : Convert.ToDateTime(dr["FDate"]).ToString("yyyy-MM-dd");
                        }
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Boolean isOK = false;
                CustBussinessopp model = new CustBussinessopp();
                //model.FNumber = this.txtCode.Text.Trim();
                model.FComName = this.txtName.Text.Trim();
                model.FStrength = this.txtStrength.Text.Trim();
                model.FAddress = this.txtAddress.Text.Trim();
                model.FEmpId = Convert.ToInt32(this.hidEmpId.Value.Trim());
                model.FArea = this.txtArea.Text.Trim();
                model.FChannel = this.txtChannel.Text.Trim();
                model.FProducts = this.txtProducts.Text.Trim();
                model.FWebsite = this.txtWebsite.Text.Trim();
                model.FNote = this.txtNote.Text.Trim();
                model.FDate = Convert.ToDateTime(this.txtDate.Text.Trim());
                model.FMaker = Int32.Parse(base.UserID);
                if ("add" == Request.QueryString["Type"].ToString().ToLower())
                {
                    this.txtCode.Text = bussBLL.Add(model);
                    isOK = true;
                }
                else if ("update" == Request.QueryString["Type"].ToString().ToLower())
                {
                    int leadId = 0;
                    int.TryParse(Request.QueryString["LeadId"], out leadId);
                    model.FItemId = leadId;
                    bussBLL.Update(model);
                    isOK = true;
                }
                if (isOK)
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "closeThisPage", "<script language=\"JavaScript\">parent.addWindow.close();parent.location.href = parent.location.href;</script>");
                }
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }
        }
    }
}